package com.agri.mapper;

import com.agri.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 用户Mapper接口
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据用户名查询用户信息（包含角色）
     * 使用ResultMap来处理一对多关系
     */
    @Select("SELECT u.* FROM t_user u WHERE u.username = #{username} AND u.status = 1")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "roles", column = "id",
                many = @Many(select = "com.agri.mapper.RoleMapper.selectRolesByUserId"))
    })
    User selectUserWithRolesByUsername(@Param("username") String username);

    /**
     * 根据用户ID查询用户信息（包含角色）
     */
    @Select("SELECT u.* FROM t_user u WHERE u.id = #{userId} AND u.status = 1")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "roles", column = "id",
                many = @Many(select = "com.agri.mapper.RoleMapper.selectRolesByUserId"))
    })
    User selectUserWithRolesById(@Param("userId") Long userId);

    /**
     * 根据用户名查询用户基本信息
     */
    @Select("SELECT * FROM t_user WHERE username = #{username}")
    User selectByUsername(@Param("username") String username);
}